package com.misolab.game.sbh.data.manager;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.misolab.game.sbh.data.db.ContentDBHelper;
import com.misolab.game.sbh.data.vo.LevelDatas;
import com.misolab.game.sbh.util.Utility;
import com.misolab.game.sbh.util.log.Logger;

public class DataManager {

	final static String ClassName = DataManager.class.getSimpleName();

	static void logFunc(String functionName) {
		Logger.i(ClassName + "::" + functionName+"()");
	}

	ContentDBHelper mDB;
	Context mContext;

	public DataManager(Context context) {
		super();
		logFunc("DataManager");

		this.mContext = context;
		mDB = new ContentDBHelper(context);
	}


	public String getContentWithLevel(int level) {
		logFunc("getContentWithLevel");
		return mDB.getContentWithLevel(level);
	}

	public boolean isReady() {
		logFunc("isReady");
		return mDB.getCount()>0;
	}

	public int setConetntData(String content) {
		logFunc("setConetntData");
		Logger.d("content:" + content);
		
		int retValue = 0;

		if (Utility.getInstance().isEmpty(content)) {
			return retValue;
		}

		try {
			ArrayList<LevelDatas> items = new ArrayList<LevelDatas>();
			
			JSONObject object = new JSONObject(content);
			JSONObject contentObject = object.getJSONObject("content");
			for (int i = 0; i < 5; i++) {
				JSONArray level = contentObject.getJSONArray(String.format("level_%d",	1 + i));
				ArrayList<String> contentList = new ArrayList<String>();
				for (int j = 0; j < level.length(); j++) {
					contentList.add(level.getString(j));
					retValue++;
				}
				items.add(new LevelDatas(i+1, contentList));
			}
			mDB.setData(items);
		} catch (JSONException e) {
			retValue= -1;
			e.printStackTrace();
		} finally{
			
		}

		return retValue;
	}
}
